package com.zwface.demo.function.web;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.StringUtils;
import com.zwface.demo.R;
import com.zwface.demo.base.BaseActivity;
import com.zwface.demo.comm.comm;
import com.zwface.demo.databinding.ActivityWebBinding;
import com.zwface.demo.http.HttpCallback;
import com.zwface.demo.http.ZWAsyncHttpClient;
import com.zwface.demo.utils.DialogHelper;
import com.zwface.demo.utils.SharedPreferenceUtils;


public class WebActivity extends BaseActivity<ActivityWebBinding> implements View.OnClickListener {

    private int type;
    private Boolean needAgreeBtn;
    public static void start(Context context,String title,int type)
    {
        Intent intent = new Intent(context,WebActivity.class);
        intent.putExtra("title",title);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    public static void start(Context context,String title,String url)
    {
        Intent intent = new Intent(context,WebActivity.class);
        intent.putExtra("title",title);
        intent.putExtra("url",url);
        context.startActivity(intent);
    }

    public static void start(Activity context,String title,int type,Boolean needAgreeBtn,int requestCode)
    {
        Intent intent = new Intent(context,WebActivity.class);
        intent.putExtra("title",title);
        intent.putExtra("type",type);
        intent.putExtra("needAgreeBtn",needAgreeBtn);
        context.startActivityForResult(intent,requestCode);
    }

    @Override
    public int onLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    public void onInitData(Bundle savedInstanceState) {
        super.onInitData(savedInstanceState);
        needAgreeBtn = getIntent().getBooleanExtra("needAgreeBtn",false);
        setResult(RESULT_OK);
        initTopbar();
        init();
    }

    private void initTopbar()
    {
        String title = getIntent().getStringExtra("title");
        binding.topbar.setTitle(title);
        if(!needAgreeBtn)
            binding.topbar.addLeftBackImageButton().setOnClickListener(view -> finish());
    }

    private void init()
    {
        binding.btnCustomer.setOnClickListener(this);
        type = getIntent().getIntExtra("type",0);
        String url = getIntent().getStringExtra("url");
        if(StringUtils.isEmpty(url))
        {
            getDetail(type);
        }else {
            //支持javascript
            binding.webView.getSettings().setJavaScriptEnabled(true);
            //扩大比例的缩放
            binding.webView.getSettings().setUseWideViewPort(false);
            //自适应屏幕
            binding.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            binding.webView.getSettings().setLoadWithOverviewMode(true);
            binding.webView.loadUrl(url);
        }

        if(needAgreeBtn){
            binding.btnAgree.setVisibility(View.VISIBLE);
            binding.btnAgree.setOnClickListener(this);
        }
    }

    private void getDetail(int type)
    {
        String url = comm.API_GET_AGREE_CONTENT+"/"+type;
        ZWAsyncHttpClient.get( url, null, new HttpCallback() {
            @Override
            public void OnSuccess(int i, String res) {
                JSONObject object = JSONObject.parseObject(res);
                String content = object.getString("content");
                WebviewUtils.loadWebview(binding.webView,content);
            }
            @Override
            public void OnFailure(int i, String res) {
                showToast(res);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_customer:
                DialogHelper.showCallDialog((Activity) context,"");
                break;
            case R.id.btn_agree:
                setResult(RESULT_OK);
                if(type==comm.AGREEMENT_TYPE_REGISTER)
                    SharedPreferenceUtils.setParam(context,SharedPreferenceUtils.AGREEMENT_USER,"read");//用户协议
                else if(type==comm.AGREEMENT_TYPE_PRIVATE_POLICY)
                    SharedPreferenceUtils.setParam(context,SharedPreferenceUtils.AGREEMENT_PRIVACY,"read");//隐私协议
                finish();
                break;
        }
    }
}
